In this code sentiment analysis is conducted using the Sentida package. Additionally, the development in sentiment over time is plotted using the gganimatepackage.
# load packages
library(pacman)
p_load(rlist, Sentida, ggplot2, tidyverse, gganimate, gifski)
# set theme
theme_set(theme_bw())
# load data
tweets_before <- read.csv("tweets_before.csv")
tweets_after <- read.csv("tweets_after.csv")
The sentiment_func calculates the total sentiment per tweet and add this value in a new column.
The date_func cleans the date column (created_at) and make it into date format, so it can be used in the gganimate plots and returns a dataframe where the column has been cleaned.
The mean_sentiment_pr_day calculates the mean sentiment per unique date in the dataset
# sentiment function
sentiment_func <- function(df){
sentiment <- c()
for (i in df$full_text) {
l = sentida(i, output = "total")
sentiment = list.append(sentiment, l)
}
df <- cbind(df,sentiment)
return(df)
}
# date format function
date_func <- function(df){
df$created_at <- str_replace_all(df$created_at, "\\ .*", "")
df$created_at <- str_replace_all(df$created_at, "2020-", "20-")
df$created_at <- as.Date(df$created_at)
return(df)
}
# get average sentiment pr unique dates
mean_sentiment_pr_day =
function(df){
summary = df %>% group_by(created_at) %>% summarise(mean_sentiment=mean(sentiment),day=min(created_at)) %>% select(-created_at)
return(summary)
}
dkgreen_before <- dkgreen_before %>% sentiment_func() %>% date_func()
dkgreen_after <- dkgreen_after %>% sentiment_func() %>% date_func()
dkpol_before <- dkpol_before %>% sentiment_func() %>% date_func()
dkpol_after <- dkpol_after %>% sentiment_func() %>% date_func()
dkgreen_before_sum <- dkgreen_before %>% mean_sentiment_pr_day()
## `summarise()` ungrouping output (override with `.groups` argument)
dkgreen_after_sum <- dkgreen_after %>% mean_sentiment_pr_day()
## `summarise()` ungrouping output (override with `.groups` argument)
dkpol_before_sum <- dkpol_before %>% mean_sentiment_pr_day()
## `summarise()` ungrouping output (override with `.groups` argument)
dkpol_after_sum <- dkpol_after %>% mean_sentiment_pr_day()
## `summarise()` ungrouping output (override with `.groups` argument)